package com.lhy.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lhy.admin.common.mvcplus.service.SimpleService;
import com.lhy.admin.mapper.UserMapper;
import com.lhy.admin.model.User;
import com.lhy.admin.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService extends SimpleService<UserMapper, User> {

    public User getUserByNameOrEmailOrPhone(String cond) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).eq(User::getStatus, 1);
        wrapper.and(wrapper1 -> wrapper1.eq(User::getName, cond).or().eq(User::getEmail, cond).or().eq(User::getPhone, cond));
        List<User> users = this.list(wrapper);
        if (users.size() == 0) {
            return null;
        }
        return users.get(0);
    }

    public User login(String cond, String password) {
        User user = getUserByNameOrEmailOrPhone(cond);
        if (user == null) {
            return null;
        }
        String s = StringUtil.md5(password, user.getName());
        boolean b = s.equalsIgnoreCase(user.getPass());
        if (b) {
            return user;
        }
        return null;
    }

}
